package com.learning.jquizenglish.screen.doquiz;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Toast;

import com.learning.jquizenglish.JQuizAppConfig;
import com.learning.jquizenglish.R;
import com.learning.jquizenglish.activity.DoQuizActivity;
import com.learning.jquizenglish.common.JQuizNoteManagement;
import com.learning.jquizenglish.common.fragment.JQuizEnglishFragment;
import com.learning.jquizenglish.model.JQuizRequestModel;
import com.learning.jquizenglish.model.QuestionModel;
import com.learning.jquizenglish.model.ResultModel;
import com.learning.jquizenglish.network.JQuizEnglishApi;
import com.learning.jquizenglish.utility.DialogUtil;
import com.learning.jquizenglish.utility.DialogUtil.OnDialogListener;
import com.learning.jquizenglish.widget.MultiDirectionSlidingDrawer;

public class DoQuizScreen extends JQuizEnglishFragment{

	public QuestionNumberView 	mQuestionNumberView;
	public DoQuizView 			mDoQuizView;
	public ResultQuizView 		mResultQuizView;
	public DoQuizMode			mDoQuizMode = DoQuizMode.CHOOSE_QUESTION_NUMBER;
	private JQuizNoteManagement mJQuizNoteManagement;
	private MultiDirectionSlidingDrawer mDrawer;
	private View parentView;
	@Override
	protected View onCreateContentView(LayoutInflater inflater,
			ViewGroup container) {
		parentView = inflater.inflate(R.layout.screen_do_quiz, container, false);
		mQuestionNumberView = new QuestionNumberView(this);
		mDoQuizView			= new DoQuizView(this);
		mResultQuizView		= new ResultQuizView(this);
		initSlidingDrawer();
		changeMode(mDoQuizMode);
		return parentView;
	}
	
	public void setMode(DoQuizMode mode){
		mDoQuizMode = mode;
	}
	
	private void initSlidingDrawer(){
		mDrawer = (MultiDirectionSlidingDrawer) parentView.findViewById(R.id.drawer);
		mJQuizNoteManagement = new JQuizNoteManagement();
		mJQuizNoteManagement.create(getActivity(), parentView);
	}
	
	public void changeMode(DoQuizMode mode){
		switch (mode) {
		case CHOOSE_QUESTION_NUMBER:
			hideHeaderMore();
			parentView.findViewById(R.id.layout_choose_question).setVisibility(View.VISIBLE);
			parentView.findViewById(R.id.layout_do_quiz).setVisibility(View.GONE);
			parentView.findViewById(R.id.layout_result).setVisibility(View.GONE);
			mQuestionNumberView.init(getActivity(), parentView.findViewById(R.id.layout_choose_question));
			break;
		case DO_QUIZ:
			showHeaderMore();
			parentView.findViewById(R.id.layout_choose_question).setVisibility(View.GONE);
			parentView.findViewById(R.id.layout_do_quiz).setVisibility(View.VISIBLE);
			parentView.findViewById(R.id.layout_result).setVisibility(View.GONE);
			mDoQuizView.init(getActivity(), parentView.findViewById(R.id.layout_do_quiz));
			break;
		case RESULT:
			hideHeaderMore();
			parentView.findViewById(R.id.layout_choose_question).setVisibility(View.GONE);
			parentView.findViewById(R.id.layout_do_quiz).setVisibility(View.GONE);
			parentView.findViewById(R.id.layout_result).setVisibility(View.VISIBLE);
			mResultQuizView.setData(DoQuizSession.getInstance().getListQuestions());
			if (mDoQuizMode == DoQuizMode.RESULT) {
				mResultQuizView.init(getActivity(),  parentView.findViewById(R.id.layout_result));

			}else{
				mResultQuizView.saveScore(getActivity(), parentView.findViewById(R.id.layout_result));
			}
			break;
		default:
			break;
		}
	}
	
	public enum DoQuizMode{
		CHOOSE_QUESTION_NUMBER,
		DO_QUIZ,
		RESULT
	}
	
	
	@Override
	public void onComment() {
		final QuestionModel model = mDoQuizView.getCurrentQuestion();
		DialogUtil.showCommentDialog(getActivity(), model, null);
	}
	
	@Override
	public void onHelp() {
		QuestionModel model = mDoQuizView.getCurrentQuestion();
		DialogUtil.showHelpDialog(getActivity(), model, null);
	}
	
	@Override
	public void onDestroy() {
		DoQuizSession.getInstance().clearSession();
		mJQuizNoteManagement.saveJquizNote();
		mDoQuizView.onDestroy();
		if (JQuizAppConfig.IS_LISTENING_APP) {
			((DoQuizActivity)getActivity()).stopAudio();
		}
		super.onDestroy();
	}
}
